<?php

class ArticleController extends Controller {

    public function actionIndex() {
        $this->render('index');
    }

    public function actionList($catid) {
        $language = Yii::app()->language;
        $criteria = new CDbCriteria();
        $criteria->addCondition('category_id = :category_id');
        $criteria->params = array(':category_id' => $catid);
        
        $data = array();
        if ($language == 'en') {
            $data = Articleen::model()->getNewsWithCondition($criteria);
        } else {
            $data = Articlevn::model()->fgetNewsWithCondition($criteria);
        }
        $this->render('list', $data);
    }

    public function actionView($keyword) {

        $article = null;
        $language = Yii::app()->language;
        if (!isset($language)) {
            $language = 'en';
        }

        $criteria = new CDbCriteria;
        $criteria->addCondition('keyword = :keyword');
        $criteria->params = array(':keyword' => $keyword);

        switch ($language) {
            case 'vn':
                $article = new Articlevn();
                $article = Articlevn::model()->find($criteria);
                $this->relatedNews = Articlevn::model()->getRelatedNews($article->category_id);
                break;
            default :
                $article = new Articleen();
                $article = Articleen::model()->find($criteria);
                $this->relatedNews = Articleen::model()->getRelatedNews($article->category_id);
        }
        $this->pageTitle = $article->title;
        $this->render('view', array('article' => $article));
    }

    public function actionSearch() {
        $articleSearch = new Search;
        if (isset($_GET['Search'])) {
            $articleSearch->attributes = $_GET['Search'];
            $keyWord = $articleSearch->keyWord;
            if (strlen($keyWord) > 0) {
                $data = array();
                $criteria = new CDbCriteria;
                $criteria->addCondition('title like :keyWord');
                $criteria->params = array(':keyWord' => '%' . $keyWord . '%');

                $language = $this->getCurrentLanguage();
                switch ($language) {
                    case 'vn':
                        $data = Articlevn::model()->getNewsWithCondition($criteria);
                        $this->relatedNews = Articlevn::model()->getRelatedNews();
                        break;
                    default :
                        $data = Articleen::model()->getNewsWithCondition($criteria);
                        $this->relatedNews = Articleen::model()->getRelatedNews();
                }
                $this->pageTitle = Yii::t('default', 'Search article');
                $this->render('list', $data);
            } else {
                $this->redirect(Yii::app()->baseUrl);
            }
        }
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}